<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="Entity.stu" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>首页</title>
    <link rel="stylesheet" href="/app/Pages/css/a.css">
    <link rel="stylesheet" href="/app/Pages/css/bootstrap.min.css">
</head>


<body>
<h1 style="font-family: '宋体';font-size: 25px">欢迎你！<%=request.getSession().getAttribute("user")%> 当前在线人数：<%= application.getAttribute("count") %> 当前页共：${list.size()} 条数据</h1>
<a href="/app/Pages/add.jsp" class="btn btn-primary"><i class="icon-plus"></i> 添加</a>
<table>
    <thead>
    <tr align="center">
        <th >id</th>
        <th >姓名</th>
        <th >年龄</th>
        <th >性别</th>
        <th >操作</th>
    </tr>
    </thead>
    <c:forEach items="${list}" var="item">
        <tr>
            <td>${item.id}</td>
            <td>${item.name}</td>
            <td>${item.age}</td>
            <td>${item.sex}</td>
            <td>
                <div class="button-group">
                    <button class="table-button btn-delete" onclick="deletes(${item.id})">删除</button>
                    <button class="table-button btn-edit" onclick="bj(${item.id})">编辑</button>
                </div>
            </td>
        </tr>
    </c:forEach>
</table>
<button style="margin-left: 850px" onclick="location.href = '/app/api/testpost?page='+<%=Integer.parseInt(request.getParameter("page"))==1?1:Integer.parseInt(request.getParameter("page"))-1%>">上一页</button><span>当前第<%=request.getParameter("page")==null?"1":request.getParameter("page")%>页</span><button onclick="location.href = '/app/api/testpost?page='+<%=Integer.parseInt(request.getParameter("page"))+1%>">下一页</button>
</body>
<script>
    function bj(id){
        location.href = '/app/api/edit?id='+id
    }
    function deletes(id){
        const headers = new Headers();
        const options = {
            method: 'GET',
            headers: headers
        };
        fetch('http://localhost:8080/app/api/delete?id='+id, options)
            .then(response => response.json())
            .then(result => {
                if(result.code == 200){
                    alert(result.msg)
                    setTimeout(() => {
                        location.reload()
                    },100)
                }else{
                    alert(result.msg)
                }
            })
            .catch(error => console.log('error', error));
    }
    function fy(page){
        location.href = "/app/api/testpost?page="+page;
    }
</script>
</html>
